--[[
	This *PROPER* way of checking if the Player has a spell
		was donated by Diesal.
]]
spellBook = setmetatable({ [BOOKTYPE_SPELL] = {}, [BOOKTYPE_PET] = {}, flyout = {}},
{__index = {	
	['Update'] = function(self)
		wipe(self[BOOKTYPE_SPELL])
		wipe(self[BOOKTYPE_PET])
		wipe(self.flyout)
		self:UpdateBook(BOOKTYPE_SPELL)
		self:UpdateBook(BOOKTYPE_PET)
		self:UpdateFlyout()
	end,
	['UpdateFlyout'] = function(self)
		for f = 1, GetNumFlyouts() do
			local flyoutID = GetFlyoutID(f)
			local _, _, numSlots, isKnown = GetFlyoutInfo(flyoutID)
			if isKnown and numSlots > 0 then
				for s = 1, numSlots do
					local spellID, _, isKnownSpell = GetFlyoutSlotInfo(flyoutID, s)
					local name = GetSpellInfo(spellID)
					if name and isKnownSpell then
						self.flyout[name] = true
					end
				end
			end
	  	end
	end,
	['UpdateBook'] = function(self,bookType)
		local _, _, offset, numspells = GetSpellTabInfo(GetNumSpellTabs())
		local book = self[bookType]
		for bookID = 1, offset + numspells do
			local type, spellID = GetSpellBookItemInfo(bookID, bookType)
			if type == "SPELL" then
				local SpellBookItemName = GetSpellBookItemName(bookID, bookType)
				local baseSpellName = GetSpellInfo(spellID)
				if SpellBookItemName then book[SpellBookItemName] = bookID end
				if baseSpellName then book[baseSpellName] = bookID end
			end
		end
	end,
	['GetSpellInfo'] = function(self, spell)
		local spellname = GetSpellName(spell)
		local index = self[BOOKTYPE_SPELL][spellname]
		if index then return spellname, index, BOOKTYPE_SPELL else return false end
		index = self[BOOKTYPE_PET][spellname]
		if index then return spellname, index, BOOKTYPE_PET else return false end
		index = self.flyout[spellname]
		if index then return spellname, index else return false end
	end,
}})

function GetSpellID(spell)
	if type(spell) == 'number' then return spell end
	return (GetSpellLink(spell) or ''):match('Hspell:(%d+)|h')  
end
function GetSpellName(spell)
	local spellID = tonumber(spell)
	if spellID then return GetSpellInfo(spellID) end
	return spell
end